package cn.com.api.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_discover_comment_reply;
import cn.com.api.service.ApiU_discover_commentService;
import cn.com.api.service.ApiU_discover_comment_replyService;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_discover_comment_reply/")
public class ApiU_discover_comment_replyController {
    @Autowired
    private ApiU_discover_comment_replyService apiU_discover_comment_replyService;
    
    @Autowired
    private ApiU_discover_commentService apiU_discover_commentService;
	
	private static Logger logger = Logger.getLogger(ApiU_discover_comment_replyController.class);
	
	/**
	 * 回复用户信息增加
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "insertU_discover_comment_reply")
	public void insertU_discover_comment_reply(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String comment_id = request.getParameter("comment_id");
		String content = request.getParameter("content");
		String user_id = request.getParameter("user_id");
		String user_name = request.getParameter("user_name");
		String reply_id = request.getParameter("reply_id");
		String reply_user_id = request.getParameter("reply_user_id");
		String reply_user_name = request.getParameter("reply_user_name");
		String is_effect = request.getParameter("is_effect");
		String is_delete = request.getParameter("is_delete");
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			
				ApiU_discover_comment_reply u_discover_comment_reply = new ApiU_discover_comment_reply();
				
    		    u_discover_comment_reply.setComment_id(comment_id);
    		    u_discover_comment_reply.setContent(content);
    		    u_discover_comment_reply.setUser_id(user_id);
    		    u_discover_comment_reply.setUser_name(user_name);
    		    u_discover_comment_reply.setReply_id(reply_id);
    		    u_discover_comment_reply.setReply_user_id(reply_user_id);
    		    u_discover_comment_reply.setReply_user_name(reply_user_name);
    		    u_discover_comment_reply.setCreate_time(sd.format(new Date()));
    		    u_discover_comment_reply.setIs_effect(0);
    		    u_discover_comment_reply.setIs_delete(0);
				
				apiU_discover_comment_replyService.insertU_discover_comment_reply_condition(u_discover_comment_reply);
				
				apiU_discover_commentService.updateU_discover_comment_count(comment_id);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "回复成功！");
			
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	
	
	 /**
		 * 根据活动ID查询评论列表
		 * 
		 * @param request
		 * @param response
		 * @return
		 */
		@RequestMapping("/findU_discover_comments_replay")
		public void findU_discover_comments_replay(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo, String user_id,
				String token) throws Exception {
		
			Map<String,Object> map = new HashMap<String,Object>();
			Map<String,Object> replay_map = new HashMap<String,Object>();
			List<Map<String,Object>> result_list = new ArrayList<Map<String,Object>>();
			Map<String,Object> param = new HashMap<String,Object>();
			String comment_id = request.getParameter("comment_id");
			try{
				param.put("comment_id", comment_id);
				param.put("user_id", user_id);

			    List<Map<String,Object>> reply_list = apiU_discover_comment_replyService.findU_discover_comment_replysByMap((HashMap)param);
			    for (Map<String, Object> map2 : reply_list) {
			    	
			    	replay_map = new HashMap<String,Object>();
			    	replay_map.put("user_id", map2.get("USER_ID"));
			    	replay_map.put("user_name", map2.get("USER_NAME"));
			    	replay_map.put("content", map2.get("CONTENT"));
			    	replay_map.put("create_time", map2.get("CREATE_TIME"));
			    	replay_map.put("zan_count", map2.get("ZAN_COUNT"));
			    	replay_map.put("c_avatar", map2.get("C_AVATAR"));
			    	replay_map.put("reply_user_name", map2.get("REPLY_USER_NAME"));
			    	replay_map.put("is_zan", map2.get("IS_ZAN"));
			    	result_list.add(replay_map);
				}
			    
				// return
	            map.put("datas", result_list);
	            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "查询成功！");
			}catch(Exception e){
				logger.error(e);
	            e.printStackTrace();  
	    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
	    		map.put(Utils.MESSAGE, "系统异常！");
			}

			JsonUtil.printJson(response, JsonUtil.object2json(map));
	        
		}
 
}


